﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Threading;
using System.Timers;

namespace WuHouCiClient
{

    public class start_link
    {
        //单列
        private static start_link LinkManager = null;
        public static start_link getInstance()
        {
            if (LinkManager == null)
            {
                LinkManager = new start_link();
                return LinkManager;
            }
            else
            {
                return LinkManager;
            }
        }




        private TcpClient p_client_;

        public void setLink(TcpClient source)
        {
            this.p_client_ = source;
        }

        public void setLink()
        {
            this.p_client_ = null;
        }

        TcpClient getLink()
        {
            return this.p_client_;
        }

        public bool is_link()
        {
            if (this.p_client_ == null)
                return false;
            else
                return true;
        }


        //
        /// <summary>
        /// 链接服务器,ip,port读取配置类,
        /// link_count:如果链接不上会重连直到链接次数用完为止
        /// interate_time:每次链接的间隔时间
        /// </summary>
        /// <param name="link_count"></param>
        /// /// <param name="interate_time"></param>
        /// <returns></returns>
        /// 
        public void conect(int link_count, int interate_time)
        {
            //创建链接，
            client_config cconfig = new client_config();
            string ip = cconfig.getIP();
            int port = cconfig.getPort();
            int index = 1;
            flag: Console.WriteLine(ip);
            Console.WriteLine(port);

            TcpClient clientSocket = CNet.createConnect(ip, port);

            if (clientSocket == null)
            {
                Console.WriteLine("connect error!");
                if (index <= link_count)
                {
                    index++;
                    Thread.Sleep(interate_time);
                    goto flag;
                }
                else
                {
                    Console.WriteLine("reconnect end!  connect failed----list_count = " + index);
                    return;
                }
            }

            this.setLink(clientSocket);
        }
        //用于每一帧刷新
        public void refresh()
        {
            if (this.is_link())
            {
                TcpClient server_socket = this.getLink();
                CProtocol pro_instance = CProtocol.getInstance(server_socket);

                int readNum = 0;
                byte[] message = CNet.recvPackage(ref readNum, server_socket);
                if (message.Length > 0)
                {
                    pro_instance.pushRecvBuffer(message);
                }
            }
            else
            {
                this.link_break_callback();
            }
        }
        //注册到消息管理器
        public void regist()
        {
            Register_login register_l = new Register_login();
            CNet cnet_instance = CNet.getInstance();

            cnet_instance.register_client(register_l);

        }

        //回调,当链接不存在是客户端使用
        void link_break_callback()
        {

        }
    }
}
